import { LockOutlined, UserOutlined } from '@ant-design/icons';
import {
  LoginForm,
  ProFormCaptcha,
  ProFormCheckbox,
  ProFormText,
} from '@ant-design/pro-components';
import { Alert, message, Tabs } from 'antd';
import React, { useEffect, useState } from 'react';
import { FormattedMessage, history, SelectLang, useIntl, useModel } from 'umi';
import styles from './index.less';
import { createImgMsg, login, getCurrentUserMenu } from '@/services/api/user';
import { handleaccessData } from '@/access.ts';
const LoginMessage: React.FC<{
  content: string;
}> = ({ content }) => (
  <Alert
    style={{
      marginBottom: 24,
    }}
    message={content}
    type="error"
    showIcon
  />
);

const Login: React.FC = () => {
  const [userLoginState, setUserLoginState] = useState<API.LoginResult>({});
  const [type, setType] = useState<string>('account');
  const { initialState, setInitialState } = useModel('@@initialState');
  const [imgData, setimgData] = useState({
    png: '',
    verificationcode: '',
  });
  const intl = useIntl();
  // useEffect(() => {
  //   getImg();
  // }, []);
  const getImg = () => {
    createImgMsg().then((res) => {
      setimgData(res?.data);
    });
  };
  const fetchUserInfo = async (userInfo: any) => {
    if (userInfo) {
      await setInitialState((s) => ({
        ...s,
        currentUser: userInfo,
      }));
    }
  };

  const handleSubmit = async (values: any) => {
    try {
      console.log(values);
      // 登录
      const msg: any = await login({
        ...values,
      });
      if (msg.code === '0') {
        const defaultLoginSuccessMessage = intl.formatMessage({
          id: 'pages.login.success',
          defaultMessage: '登录成功！',
        });
        message.success(defaultLoginSuccessMessage);
        console.log(msg.data);
        window.localStorage.setItem('userInfo', JSON.stringify(msg.data))
        await fetchUserInfo(msg.data);
        /** 此方法会跳转到 redirect 参数所在的位置 */
        history.push('/admin/home');
        return;
      }
      console.log(msg);
      // 如果失败去设置用户错误信息
      setUserLoginState(msg);
      // getImg();
    } catch (error) {
      // const defaultLoginFailureMessage = intl.formatMessage({
      //   id: 'pages.login.failure',
      //   defaultMessage: '登录失败，请重试！',
      // });
      // message.error(defaultLoginFailureMessage);
      // getImg();
    }
  };
  return (
    <div className={styles.container}>
      <div className={styles.lang} data-lang>
        {SelectLang && <SelectLang />}
      </div>
      <div className={styles.content}>
        <div style={{ margin: '20px', textAlign: 'center', fontSize: '30px', fontWeight: 'bold' }}>
          数据百科管理平台
        </div>

        <LoginForm
          // logo={<img alt="logo" src="/logo.svg" />}
          initialValues={{
            autoLogin: true,
          }}
          onFinish={async (values) => {
            await handleSubmit(values as API.LoginParams);
          }}
        >
          <ProFormText
            name="account"
            fieldProps={{
              size: 'large',
              prefix: <UserOutlined className={styles.prefixIcon} />,
            }}
            rules={[
              {
                required: true,
                message: '请输入用户名',
              },
            ]}
          />
          <ProFormText.Password
            name="password"
            fieldProps={{
              size: 'large',
              prefix: <LockOutlined className={styles.prefixIcon} />,
            }}
            rules={[
              {
                required: true,
                message: '请输入密码！',
              },
            ]}
          />

        </LoginForm>
      </div>
      {/* <Footer /> */}
    </div>
  );
};

export default Login;
